<template>
  <van-action-sheet @close="close" v-model="show" title="频道管理">
    <div class="content">
      <!-- 标题 -->
      <div class="title">
        <span>我的频道</span>
        <van-button
          type="danger"
          plain
          size="small"
          @click="isEdit = !isEdit"
          >{{ isEdit ? '完成' : '编辑' }}</van-button
        >
      </div>
      <!-- 宫格 -->
      <van-grid>
        <van-grid-item
          v-for="(item, index) in ownList"
          :key="item.id"
          @click="intoChannel(index)"
        >
          <span :class="{ active: index == value }">{{ item.name }}</span>
          <van-icon
            class="del-icon"
            name="cross"
            v-show="isEdit && index != 0"
            @click="delList(index)"
          />
        </van-grid-item>
      </van-grid>
    </div>

    <!-- 可选频道 -->
    <div class="content">
      <!-- 标题 -->
      <div class="title">
        <span>可选频道</span>
      </div>
      <!-- 宫格 -->
      <van-grid>
        <van-grid-item
          v-for="item in optionalList"
          :key="item.id"
          :text="item.name"
          @click="addChannel(item)"
        />
      </van-grid>
    </div>
  </van-action-sheet>
</template>

<script>
import { saveChannelAPI } from '@/api/channel'
export default {
  props: {
    allList: {
      type: Array,
      required: true
    },
    ownList: {
      type: Array,
      required: true
    },
    //高亮选中的下标由父组件传进来
    value: {
      type: Number,
      default: 0,
      required: true
    }
  },
  data () {
    return {
      show: false,
      //控制是否编辑的图标
      isEdit: false
    }
  },
  methods: {
    intoChannel (v) {
      // console.log('父组件里面的',v)
      this.$emit('input', v)
      //关闭actionsheet
      this.show = false
    },
    //delList
    async delList (index) {
      // console.log('dellist', index, this.value)
      //从ownlist数组中删除对应下标的数组即可
      this.ownList.splice(index, 1)
      if (index < this.value) {
        this.$emit('input', this.value - 1)
      }

      //判断有没有登录
      if (this.$store.state.tokenObj.token) {
        //发送请求
        //用map抽取一个一样长度的数组对象，每一项有两个属性
        const channels = this.ownList.map((v, i) => {
          return {
            id: v.id,
            seq: i
          }
        })
        //拿到数组发请求
        await saveChannelAPI({
          channels
        })
      } else {
        //代表没有登录
        //那就保存到本地
        localStorage.setItem('channels', JSON.stringify(this.ownList))
      }
    },
    //添加频道
    async addChannel (item) {
      //加到我的数组里面
      this.ownList.push(item)

      //判断有没有登录
      if (this.$store.state.tokenObj.token) {
        //发送请求
        //用map抽取一个一样长度的数组对象，每一项有两个属性
        const channels = this.ownList.map((v, i) => {
          return {
            id: v.id,
            seq: i
          }
        })
        //拿到数组发请求
        await saveChannelAPI({
          channels
        })
      } else {
        //代表没有登录
        //那就保存到本地
        localStorage.setItem('channels', JSON.stringify(this.ownList))
      }
    },
    close () {
      // console.log('关闭了')
    }
  },
  computed: {
    //是一个函数
    optionalList () {
      //函数调用必须return结果
      //等于-1我才过滤出来，说明不包含在里面
      return this.allList.filter(
        (v) => this.ownList.findIndex(val => val.id == v.id) == -1)
    }
  }
};
</script>

<style lang="less" scoped>
.active {
  color: red;
}
.content {
  padding: 20px;
  .title {
    display: flex;
    justify-content: space-between;
  }

  .del-icon {
    position: absolute;
    right: 5px;
    top: 3px;
  }
}
</style>